﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SGUI;
using SDATA;

namespace SABI
{
    public partial class frm0414 : Form
    {
        public frm0414()
        {
            InitializeComponent();
        }

        string connectStrTEMP = "";

        private void frm0414_Load(object sender, EventArgs e)
        {
            LANG.TextForm(this, FRM.Lang);            

            cbxAuthentication.SelectedIndex = 0;
            loadDATA();
        }

        public void loadDATA()
        {
            if (FRM.DataInfo == "") { return; }
            string[] cnsArr = FRM.DataInfo.Split('|');
            cbxAuthentication.SelectedIndex = int.Parse(cnsArr[0]);
            txtServer.Text = cnsArr[1];
            //cbxListDatabase.Text = cnsArr[2];
            txtIDData.Text = cnsArr[3];
            txtPWData.Text = cnsArr[4];
            cbxListDatabase_DropDown(null, null);
            cbxListDatabase.SelectedItem = cnsArr[2];
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cbxAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxAuthentication.SelectedIndex == 0)
            {
                connectStrTEMP = "Data Source={0};Initial Catalog={1};User ID={2};Password={3}";
                txtIDData.Enabled = true;
                txtPWData.Enabled = true;
                lngDungSQLServer.Enabled = true;
                lMatKhau.Enabled = true;
            }
            else
            {
                connectStrTEMP = "Data Source={0};Initial Catalog={1};Integrated Security=True";
                txtIDData.Enabled = false;
                txtPWData.Enabled = false;
                lngDungSQLServer.Enabled = false;
                lMatKhau.Enabled = false;
            }
        }

        private void cbxListDatabase_DropDown(object sender, EventArgs e)
        {
            string cnnStr = "";
            if (cbxAuthentication.SelectedIndex == 0)
                cnnStr = string.Format(connectStrTEMP, txtServer.Text, "master", txtIDData.Text, txtPWData.Text);
            else
                cnnStr = string.Format(connectStrTEMP, txtServer.Text, "master");
            List<string> ListDatabase = DATA.GetListSimple("SELECT name FROM sys.databases d WHERE d.database_id > 4", cnnStr);
            cbxListDatabase.DataSource = null;
            cbxListDatabase.DataSource = ListDatabase;
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            string cnnStr = "";
            if (cbxAuthentication.SelectedIndex == 0)
                cnnStr = string.Format(connectStrTEMP, txtServer.Text, cbxListDatabase.Text, txtIDData.Text, txtPWData.Text);
            else
                cnnStr = string.Format(connectStrTEMP, txtServer.Text, cbxListDatabase.Text);
            if (DATA.CheckData(cnnStr))
            {
                //SAVE SETTING
                FRM.ConnectString = cnnStr;
                FRM.DataInfo = string.Format("{0}|{1}|{2}|{3}|{4}", cbxAuthentication.SelectedIndex.ToString(), txtServer.Text, cbxListDatabase.Text, txtIDData.Text, txtPWData.Text);
                FRM.SaveConfig();
                (Application.OpenForms["frm0413"] as frm0413).LoadDATA();
                Close();
            }
        }


    }
}
